/**
 * @type {import('webpack').Configuration}
 */

const webpack = require("webpack");
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const SpeedMeasureWebpackPlugin = require("speed-measure-webpack-plugin");
const BundleAnalyzerPlugin =
  require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
const OptimizeCssAssetsWebpackPlugin = require("optimize-css-assets-webpack-plugin");
const WorkboxPlugin = require("workbox-webpack-plugin");

const smp = new SpeedMeasureWebpackPlugin();

const config = {
  mode: "development",
  entry: "./src/index.js",
  output: {
    filename: "bundle.js",
    path: path.join(__dirname, "dist"),
    publicPath: "./",
  },
  devServer: {
    contentBase: path.resolve(__dirname, "public"),
    compress: true,
    port: 8080,
  },
  // devtool: 'source-map',
  resolve: {
    alias: {
      "@": path.join(__dirname, "src"),
    },
    extensions: ["js", "json"],
    modules: [path.join(__dirname, "src"), "node_modules"],
  },
  // resolveLoader: {
  //     modules: ['node_modules', path.join(__dirname, 'loader')]
  // },
  // externals: {
  //     jquery: 'jQuery'
  // },
  // cache: {
  //     type: 'filesystem'
  // },
  module: {
    // noParse: /jquery/,
    rules: [
      {
        test: /\.css$/i,
        use: [
          // 'style-loader',
          MiniCssExtractPlugin.loader,
          // 'cache-loader',
          "css-loader",
          "postcss-loader",
          "sass-loader",
        ],
      },
      {
        test: /\.(jpe?g|png|gif)$/i,
        type: "asset",
        generator: {
          filename: "[name][hash:8][ext]",
        },
        parser: {
          dataUrlCondition: {
            maxSize: 50 * 1024,
          },
        },
      },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/i, // 匹配字体文件
        type: "asset",
        generator: {
          filename: "[name][hash:8][ext]",
        },
        parser: {
          dataUrlCondition: {
            maxSize: 10 * 1024,
          },
        },
      },
      {
        test: /\.js$/i,
        include: path.join(__dirname, "src"),
        exclude: /node_modules/,
        use: [
          {
            loader: "thread-loader",
            options: {
              worker: 3,
            },
          },
          {
            loader: "babel-loader",
            options: {
              cacheDirectory: true,
            },
          },
        ],
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "./src/index.html",
    }),
    new CleanWebpackPlugin(),
    new MiniCssExtractPlugin({
      // 添加插件
      filename: "[name].[hash:8].css",
    }),
    // new webpack.IgnorePlugin({
    //     resourceRegExp: /^\.\/locale$/,
    //     contextRegExp: /moment$/,
    // }),
    // new BundleAnalyzerPlugin({
    //     // analyzerMode: 'disabled',
    //     // generateStatsFile: true
    // })
    new WorkboxPlugin.GenerateSW({
      clientsClaim: true,
      skipWaiting: true,
    }),
  ],
  // optimization: {
  //     minimize: true,
  //     minimizer: [
  //         // 添加 css 压缩配置
  //         new OptimizeCssAssetsWebpackPlugin({}),
  //     ]
  // },
};

module.exports = (env, argv) => {
  console.log("mode", argv.mode);
  return smp.wrap(config);
};
